package com.sj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sj.entity.Menu;
import com.sj.entity.Role;
import com.sj.entity.User;
import com.sj.mapper.UserMapper;
import com.sj.service.MenuService;
import com.sj.service.RoleService;
import com.sj.service.UserService;
import com.sj.utils.RedisUtil;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小七
 * @since 2021-07-30
 */
@Service
@ConfigurationProperties(prefix = "sjblog.key")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Setter
    private String authorityKey;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Override
    public User getUserByUsername(String username) {
        return getOne(new QueryWrapper<User>().eq("username", username));
    }

    @Override
    public String getUserAuthorityInfo(Integer id) {

        User user = userMapper.selectById(id);

        String authority = "";

        // 如果 缓存中有该用户的权限 直接返回
        if (redisUtil.hasKey(authorityKey + user.getUsername())) {
            return (String) redisUtil.get(authorityKey + user.getUsername());
        }

        // 查询角色信息
        List<Role> roleList = roleService.list(new QueryWrapper<Role>().inSql("id", "select role_id from user_role where user_id = " + id));
        if (roleList.size() > 0) {
            String roleCodeList = roleList.stream().map(r -> "ROLE_" + r.getRoleCode()).collect(Collectors.joining(","));
            authority = roleCodeList.concat(",");
        }

        // 查询菜单权限信息
        List<Menu> menuList = menuService.getMenuListByUserId(id);
        if (roleList.size() > 0) {
            String menuPermsList = menuList.stream().map(m -> m.getPerms()).collect(Collectors.joining(","));
            authority = authority.concat(menuPermsList);
        }
        // 将权限存入redis中 防止每次请求都要查询权限浪费资源 一个小时更新
        redisUtil.set(authorityKey + user.getUsername(), authority, 60 * 60);

        return authority;
    }

    @Override
    public void clearUserAuthorityInfoByUsername(String username) {
        redisUtil.del(authorityKey + username);
    }

    @Override
    public void clearUserAuthorityInfoByRoleId(Integer roleId) {
        // 根据角色id查询 所有拥有该角色的用户
        List<User> userList = this.list(new QueryWrapper<User>().inSql("id", "select user_id from user_role where role_id = " + roleId));
        userList.forEach(u -> this.clearUserAuthorityInfoByUsername(u.getUsername()));
    }

    @Override
    public void clearUserAuthorityInfoByMenuId(Integer menuId) {
        List<User> userList = userMapper.getUserListByMenuId(menuId);
        userList.forEach(u -> this.clearUserAuthorityInfoByUsername(u.getUsername()));
    }
}
